package cn.edu.scnu.springbootcinema.service;

import cn.edu.scnu.springbootcinema.entity.Movie;
import cn.edu.scnu.springbootcinema.entity.UserFavorite;
import cn.edu.scnu.springbootcinema.mapper.MovieMapper;
import cn.edu.scnu.springbootcinema.mapper.UserFavoriteMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserFavoriteService extends ServiceImpl<UserFavoriteMapper, UserFavorite> {
    @Resource
    UserFavoriteMapper userFavoriteMapper;
    @Resource
    MovieMapper movieMapper;
    public List<Movie> getFavoriteMoviesByUserId(Integer userId) {
        QueryWrapper<UserFavorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<UserFavorite> userFavorites = userFavoriteMapper.selectList(queryWrapper);
        List<Movie> movies =new ArrayList<>();
        for (UserFavorite userFavorite : userFavorites) {
            movies.add(movieMapper.selectById(userFavorite.getMovieId()));
        }
        return movies;
    }
    public List<Integer> getFavoriteMovieIdsByUserId(Integer userId) {
        QueryWrapper<UserFavorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<UserFavorite> userFavorites = userFavoriteMapper.selectList(queryWrapper);
        List<Integer> movieIds = new ArrayList<>();
        for (UserFavorite userFavorite : userFavorites) {
            movieIds.add(userFavorite.getMovieId());
        }
        return movieIds;
    }
}
